package com.news.controller;



import com.news.model.Category;
import com.news.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //添加
    @RequestMapping("/insert")
    public String insertCategory(Category category){
        String id = UUID.randomUUID().toString().replace("-","");
        category.setId(id);
        category.setStuts("0");
        categoryService.insertCategory(category);
        return "redirect:/category/query/1/10";
    }
    // 查询
/*    @RequestMapping("/query/{pageNum}/{pageSize}")
    public String queryAll(Model com.model, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        List<Category> categories = categoryService.queryAll( pageNum, pageSize);
        System.out.println(categories);
        System.out.println("ddd");
        com.model.addAttribute("list",categories);
        return "product-list";
    }*/
    @RequestMapping("/query/{pageNum}/{pageSize}")
    public String queryAll(Model model, @PathVariable("pageNum")int pageNum,@PathVariable("pageSize") int pageSize){
        List<Category> categories = categoryService.queryAll(pageNum,pageSize);
        model.addAttribute("categories", categories);
        model.addAttribute("pageNum",pageNum);
        int pageCount = 0;
        if(categories.size() % pageSize == 0){
            pageCount = categories.size() / pageSize;
        }else{
            pageCount = categories.size() / pageSize + 1;
        }
        model.addAttribute("pageCount",pageCount);

        return "product-list";
    }
    //上下架
    @RequestMapping("/update")
    public void update(Category category){
        Category categor = categoryService.queryOne(category.getId());
        String stuts = categor.getStuts();
        int i = Integer.parseInt(stuts) ^ 1;
        categor.setStuts(String.valueOf(i));
        categoryService.update(categor);
    }
}
